/*
 * @(#)ListFormSubmissionDetailAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
     * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.form.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.form.bean.FormDataElement;
import com.dcivision.form.bean.FormRecord;
import com.dcivision.form.bean.FormSubmission;
import com.dcivision.form.dao.FormDataDAObject;
import com.dcivision.form.dao.FormDataElementDAObject;
import com.dcivision.form.dao.FormFileAttachmentDAObject;
import com.dcivision.form.dao.FormRecordDAObject;
import com.dcivision.form.dao.FormSubmissionDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.WorkflowProgress;
import com.dcivision.workflow.bean.WorkflowRecord;
import com.dcivision.workflow.dao.WorkflowProgressDAObject;
import com.dcivision.workflow.dao.WorkflowRecordDAObject;

/**
  ListFormSubmissionDetailAction.java
  This class is for list form submission
    @author          Tony Chen
    @company         DCIVision Limited
    @creation date   23/02/2003
    @version         $Revision: 1.16.2.1 $
 */

public class ListFormSubmissionDetailAction
    extends AbstractListAction {
  protected String listName = "";
  protected final static String MESSAGE_RECORD_INSERT = "common.message.record_inserted";
  protected final static String MESSAGE_RECORD_UPDATE = "common.message.record_updated";
  protected final static String MESSAGE_RECORD_COPY = "common.message.record_copied";

  public ListFormSubmissionDetailAction() {
    super();
    this.setListName("formSaveDraftList");
    this.setListFunctionName("getFormSaveDraftListBySearchSection");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return ("com.dcivision.form.dao.FormSubmissionDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return (SystemFunctionConstant.FORM_SUBMISSION);
  }

  /**
   * setPageTitle
   * set the extend page title and page path.
   * default page path/title will be created by navmode and functionCode
   */
  public void setPageTitle(HttpServletRequest request, HttpServletResponse response, ActionForm form, ActionMapping mapping, ActionForward actionForward){
    FormRecord formRecord = (FormRecord)request.getAttribute("formRecord");
    if(!Utility.isEmpty(formRecord)){
      String extendTitle = formRecord.getTitle();
      request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE, extendTitle);
      request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE_SHOW_ONLY, new Boolean(true));
    }
  }
  
  public ActionForward execute(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)
      throws ServletException {
    HttpSession session = request.getSession();
    AbstractSearchForm searchForm = (AbstractSearchForm) form;
    String opMode = searchForm.getOpMode();
    String navMode = searchForm.getNavMode();
    
    //get the formSubmissionList by formRecordID
    String formRecordID = request.getParameter("formRecordID");
    if("update".equals(request.getParameter("fromPdfStatus"))){
      addMessage(request, MESSAGE_RECORD_UPDATE);
    }else if("insert".equals(request.getParameter("fromPdfStatus"))){
      addMessage(request, MESSAGE_RECORD_INSERT);  
    }else if("copy".equals(request.getParameter("fromPdfStatus"))){
      addMessage(request, MESSAGE_RECORD_COPY);    
    }
    
    // Get current related workflow for viewer display.
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if (!Utility.isEmpty(request.getParameter("workflowRecordID"))) {
      WorkflowRecordDAObject workflowRecordDAO = new WorkflowRecordDAObject(this.getSessionContainer(request),this.getConnection(request));
      WorkflowRecord currentRelatedWFRecord = (WorkflowRecord) workflowRecordDAO.getObjectByID(Integer.valueOf(request.getParameter("workflowRecordID")));
      request.setAttribute("currentRelatedWFRecord", currentRelatedWFRecord);
    }
 
    //Set Page Title
    ListFormSubmissionForm listForm = (ListFormSubmissionForm)form;
    FormRecordDAObject formRecordDAO = new FormRecordDAObject(this.getSessionContainer(request),this.getConnection(request));
    FormRecord formRecord = (FormRecord)formRecordDAO.getByFormRecordID(listForm.getFormRecordID());
    request.setAttribute("formRecord", formRecord);
    List formSubmissionList = new ArrayList();
    
    if (GlobalConstant.OP_MODE_LIST_DELETE.equals(searchForm.getOpMode())) {
      try {
        this.deleteListData(mapping, searchForm, request, response);
        this.commit(request);
        int selectCount = 0;
        if (listForm.isDelSubmitRecordFlag()){
          selectCount = listForm.getBasicSelectedID1().length;
        }else{
          selectCount = searchForm.getBasicSelectedID().length;
        }
        addMessage(request, MESSAGE_RECORDS_DELETED, ""+selectCount);
        searchForm.setBasicSelectedID(null);
        searchForm.setOpMode(null);

        // Check for all item deletes per page
        this.getListData(mapping, searchForm, request, response);        
      } catch (ApplicationException e) {
        this.rollback(request);
        this.handleApplicationException(request, e);
        request.setAttribute(this.m_sListName, new ArrayList());
      }
    }
    FormSubmissionDAObject formSubmissionDAO = new FormSubmissionDAObject(this.getSessionContainer(request),this.getConnection(request));
    formSubmissionList = formSubmissionDAO.getFormSubmissionListBySearchSection(searchForm);      
    
    request.setAttribute("formSubmissionList", formSubmissionList);
    return super.execute(mapping, form, request, response);
  }

  public void deleteListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {

   ListFormSubmissionForm listForm = (ListFormSubmissionForm)form;
   FormSubmissionDAObject formSubmissionDAO = new FormSubmissionDAObject(this.getSessionContainer(request), this.getConnection(request));
   FormDataElementDAObject formDataElementDAO = new FormDataElementDAObject(this.getSessionContainer(request), this.getConnection(request));
   FormDataDAObject formDataDAO = new FormDataDAObject(this.getSessionContainer(request), this.getConnection(request));
   FormFileAttachmentDAObject formFileAttachmentDAO = new FormFileAttachmentDAObject(this.getSessionContainer(request), this.getConnection(request));
   WorkflowProgressDAObject workFlowProgressDAO = new WorkflowProgressDAObject(this.getSessionContainer(request), this.getConnection(request));
   
   String[] selectedID = null;
   if (listForm.isDelSubmitRecordFlag()){
     selectedID = listForm.getBasicSelectedID1();
   }else{
     selectedID = form.getBasicSelectedID();
   }  
   //delete form_submission table 

   for (int i = 0; i < selectedID.length; i++) {
     if (!Utility.isEmpty(selectedID[i])) {     
      //delete Form_Data_Element and Form_Data and form_file_attachment table     
      List listFormDataElement =  formDataElementDAO.getListBySubmissionID(new Integer(selectedID[i])) ;
      String[] formDataElementIDArr = new String[listFormDataElement.size()];
      FormSubmission formSubmission=(FormSubmission)formSubmissionDAO.getObjectByID(Integer.valueOf(selectedID[i]));
      Integer trackID =formSubmission.getTrackID();
      List listFormBindWorkflowProgress = workFlowProgressDAO.getListByTrackID(trackID);
      String[] workflowProgressIDArr = !Utility.isEmpty(listFormBindWorkflowProgress)?new String[listFormBindWorkflowProgress.size()]:null;
      
      for (int j = 0; j < listFormDataElement.size(); j++) {
        FormDataElement formDataElement = (FormDataElement)listFormDataElement.get(j); 
        formDataElementIDArr[j] = formDataElement.getID().toString();
      }
      
      for (int j = 0; !Utility.isEmpty(listFormBindWorkflowProgress)&&j<listFormBindWorkflowProgress.size();j++){
        WorkflowProgress workflowProgress = (WorkflowProgress)listFormBindWorkflowProgress.get(j);
        workflowProgressIDArr[j] = workflowProgress.getID().toString();
      }
      
      if(formDataElementIDArr.length > 0){
        formDataElementDAO.deleteList(formDataElementIDArr);
        formDataDAO.deleteListByFormElementDataID(formDataElementIDArr);
        formFileAttachmentDAO.deleteListByFormElementDataID(formDataElementIDArr);
      }

      if(workflowProgressIDArr!=null&&workflowProgressIDArr.length>0){
        workFlowProgressDAO.deleteList(workflowProgressIDArr);
      } 
      
     }
   }
   formSubmissionDAO.deleteList(selectedID);
 }  
 
}
